package com.sheep.jucdemo.part4;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description
 * @Author zaixianzhu
 * @Date 2022/8/1
 */
public class Atomic {

    private static AtomicLong atomicLong = new AtomicLong();
    private static Integer[] arrayOne = new Integer[]{0,1,2,3,0,5,6,0,56,0};
    private static Integer[] arrayTwo = new Integer[]{10,1,2,3,0,5,6,0,56,0};

    public static void main(String[] args) throws InterruptedException {
        Thread threadOne = new Thread(() -> {
            System.out.println("threadOne");
            for (int i = 0; i < arrayOne.length; i++) {
                if (arrayOne[i] == 0) {
                    atomicLong.incrementAndGet();
                }
            }
        });
        Thread threadTwo = new Thread(() -> {
            System.out.println("threadTwo");
            for (int i = 0; i < arrayTwo.length; i++) {
                if (arrayTwo[i] == 0) {
                    atomicLong.incrementAndGet();
                }
            }
        });

        threadOne.start();
        threadTwo.start();
        threadOne.join();
        threadTwo.join();
        System.out.println("count 0 is:"+atomicLong.get());

    }
}
